package com.xxz.terminal;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class TreeHoleStoreActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    TextView treehole_read_title;
    DBHelper dbHelper;
    SQLiteDatabase db;
    String user_id,author_name,treehole_date,store_date,treehole_diarydata,nickname,diarydata,treehole_time,author_id,motto;
    ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_hole_store);

        SharedPreferences sp = getSharedPreferences("userinformation",MODE_PRIVATE);
        user_id = sp.getString("user_id",null);
        nickname = sp.getString("nickname",null);

        dbHelper = new DBHelper(this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Intent intent = getIntent();
        int index = intent.getIntExtra("index",0);

        treehole_read_title = findViewById(R.id.treehole_read_title);

        if(index == 1){
            treehole_read_title.setText("我喜欢的");
            Cursor cursor = db.query("treehole_diary_likestore",new String[]{"author_id","treehole_date","store_date","treehole_diarydata"},"user_id=?",new String[]{user_id},
                    null,null,null);
            if (cursor.getCount()>0) {
                cursor.moveToLast();
                do {
//                author_name = cursor.getString(cursor.getColumnIndexOrThrow("author_name"));这个只能从userinformation表中获取
                    author_id = cursor.getString(cursor.getColumnIndexOrThrow("author_id"));
                    Cursor cursor2 = db.query("userinformation", new String[]{"nickname", "motto"}, "user_id=?", new String[]{author_id},
                            null, null, null);
                    if (cursor2.moveToNext()) {
                        author_name = cursor2.getString(cursor2.getColumnIndexOrThrow("nickname"));
                        motto = cursor2.getString(cursor2.getColumnIndexOrThrow("motto"));
                    }
                    treehole_date = cursor.getString(cursor.getColumnIndexOrThrow("treehole_date"));
                    store_date = cursor.getString(cursor.getColumnIndexOrThrow("store_date"));
                    treehole_diarydata = cursor.getString(cursor.getColumnIndexOrThrow("treehole_diarydata"));
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("nickname", author_name);
                    map.put("treehole_time", treehole_date);
                    map.put("store_date", store_date);
                    map.put("diarydata", treehole_diarydata);
                    map.put("motto", motto);
                    listItems.add(map);

                }
                while (cursor.moveToPrevious());

                SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.treehole_store_list, new String[]{"nickname", "treehole_time",
                        "store_date", "diarydata"}, new int[]{R.id.nickname_store, R.id.treehole_post_time, R.id.treehole_store_time, R.id.treehole_text});
                ListView listView = findViewById(R.id.treehole_store_listview);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(this);
                listView.setOnItemLongClickListener(this);
            }
            else{
                ListView listView = findViewById(R.id.treehole_store_listview);
                listView.setEmptyView(findViewById(R.id.treeole_like_empty));

            }//列表为空时的处理
        }
        else if(index == 2){

            treehole_read_title.setText("我投递的");
            Cursor cursor = db.query("diary_data",new String[]{"diarydata","treehole_time"},"user_id=? and treehole_status=?",
                    new String[]{user_id,"已投递"},
                    null,null,null);
            if(cursor.getCount()>0) {
                cursor.moveToLast();
                do {
                    diarydata = cursor.getString(cursor.getColumnIndexOrThrow("diarydata"));
                    treehole_time = cursor.getString(cursor.getColumnIndexOrThrow("treehole_time"));
                    motto = sp.getString("motto", null);

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("diarydata", diarydata);
                    map.put("treehole_time", treehole_time);
                    map.put("nickname", nickname);
                    map.put("motto", motto);
                    listItems.add(map);

                }
                while (cursor.moveToPrevious());

                SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.treehole_store_list, new String[]{"diarydata", "treehole_time",
                        "nickname"}, new int[]{R.id.treehole_text, R.id.treehole_post_time, R.id.nickname_store});
                ListView listView = findViewById(R.id.treehole_store_listview);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(this);
                listView.setOnItemLongClickListener(this);
            }
            else {
                ListView listView = findViewById(R.id.treehole_store_listview);
                listView.setEmptyView(findViewById(R.id.treeole_like_empty));
            }//列表为空时的处理
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        ListView listView = findViewById(R.id.treehole_store_listview);
        HashMap<String,String> map = (HashMap<String, String>) listView.getItemAtPosition(position);//获得当前list展示的数据，储存在map中，通过key取值
        diarydata = map.get("diarydata");
        treehole_time = map.get("treehole_time");
        nickname = map.get("nickname");
        motto = map.get("motto");

        Intent intent = new Intent(this,TreeHoleReadActivity.class);
        intent.putExtra("diarydata",diarydata);
        intent.putExtra("treehole_time",treehole_time);
        intent.putExtra("nickname",nickname);
        intent.putExtra("motto",motto);
        intent.putExtra("index","阅读页面");
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        ListView listView = findViewById(R.id.treehole_store_listview);
        HashMap<String,String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        db = dbHelper.getWritableDatabase();
        if(treehole_read_title.getText().toString().equals("我喜欢的")){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示").setMessage("请确认是否删除当前数据").setPositiveButton("是",new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    store_date = map.get("store_date");
                    db.delete("treehole_diary_likestore","user_id=? and store_date=?",new String[]{user_id,store_date});

                    listItems.remove(position);
                    SimpleAdapter adapter = new SimpleAdapter(TreeHoleStoreActivity.this, listItems, R.layout.treehole_store_list, new String[]{"diarydata", "treehole_time",
                            "nickname"}, new int[]{R.id.treehole_text, R.id.treehole_post_time, R.id.nickname_store});
                    ListView listView = findViewById(R.id.treehole_store_listview);
                    listView.setAdapter(adapter);
                    Toast.makeText(TreeHoleStoreActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
            }).setNegativeButton("否",null);
            builder.create().show();
        }
        else {
            treehole_time = map.get("treehole_time");//获得map中的日期数据，以便删除时将数据库记录一并删除
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示").setMessage("请确认是否撤回该投递").setPositiveButton("是",new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("treehole_status","未投递");

                    db.update("diary_data",contentValues,"user_id=? and treehole_time=?",new String[]{user_id,treehole_time});//diary表状态改变
                    db.delete("treehole_diary_likestore","author_id=? and treehole_date=?",new String[]{user_id,treehole_time});//like表值删除
                    listItems.remove(position);
                    SimpleAdapter adapter = new SimpleAdapter(TreeHoleStoreActivity.this, listItems, R.layout.treehole_store_list, new String[]{"diarydata", "treehole_time",
                            "nickname"}, new int[]{R.id.treehole_text, R.id.treehole_post_time, R.id.nickname_store});
                    ListView listView = findViewById(R.id.treehole_store_listview);
                    listView.setAdapter(adapter);
                    Toast.makeText(TreeHoleStoreActivity.this, "撤回成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(TreeHoleStoreActivity.this,MainPageActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish();
                }
            }).setNegativeButton("否",null);
            builder.create().show();
        }
        return true;//长按时不会产生单击
    }
}